﻿Chenxin.UI.HouseCensor = Chenxin.extend(Chenxin.UI.PageCommon, {
    autoInitForm: false,
    actionUrl: 'service/houseCensor.ashx',
    createGrid: function() {
        return new Chenxin.UI.Pagination({
            actionUrl: this.actionUrl + '?op=gethouselist',
            id: 'houselist',
            renderTo: 'house_table'
        });
    },


    loadHouseInfo: function() {
        var _this = this;
        Chenxin.ajax({
            type: "get",
            dataType: "text",
            paramType: 'json',
            url: this.actionUrl,
            data: { op: 'gethouseinfo', houseid: this.currentId },
            onSuccess: function(data, msg) {
                if (data.houseinfo) {
                    _this.currentHouseInfo = data.houseinfo;
                    _this.renderHouseInfo();
                }
                _this.createTraceList(data.countTrace);
            }
        });
    },
    renderHouseInfo: function() {
        var m = this.currentHouseInfo;
        var infoStr = '';
        infoStr += '<tr id="tr-house-info"><td height="81" colspan="12" class="gendan_box" >';
        infoStr += '<div class="houtab fl">';
        infoStr += '<ul class="padding5">';
        infoStr += '<li style="float:left;width:360px;"><b>' + m.Community;
        switch (m.BusinessKind) {
            case 10: //出售
                var p = (m.SalePrice_T / 10000);

                infoStr += '&nbsp;&nbsp;<span class="red20">售价：' + p.toFixed(2) + '万元</span>';
                break;
            case 20: //出租
                infoStr += '&nbsp;&nbsp;<span class="red20">租价：' + m.RentPrice + '元</span>';
                break;
            case 30: //租售
                var p = (m.SalePrice_T / 10000);

                infoStr += '&nbsp;&nbsp;<span class="red20">售价：' + p.toFixed(2) + '万元</span>';
                infoStr += '&nbsp;&nbsp;<span class="red20">租价：' + m.RentPrice + '元</span>';
                break;
        }
        infoStr += '</b></li>';
        infoStr += '<li style="float:left;width:250px;">地址：' + m.Address + ' ' + ' ' + m.BuildingName + ' ' + m.Room + '</li>';
        infoStr += '<li style="float:left;width:220px;">编号：' + m.HouseCode + '</li>'; 
        infoStr += '<li style="float:left;width:230px;">板块：' + m.Block + '</li>';
        infoStr += '<li style="float:left;width:100px;">类型：' + m.BuildingType + '</li>';
        infoStr += '<li style="float:left;width:230px;">建档日期：' + m.Adddate + '</li>';
        infoStr += '<li style="float:left;width:100px;">装修：' + m.Fitment + '</li>';
        infoStr += '<li style="float:left;width:350px;">';
        infoStr += '<div class="btn_pon_3"><a href="ShowDetailInfo.aspx?houseid=' + m.HouseId + '" target="_blank" title="查看详情" >查看详情</a></div>'
        infoStr += '<input name="btn_viewownerinfo" type="button" class="btn_pon" value="查看业主"/>';
        infoStr += '<input name="btn_trace" type="button" class="btn_pon" value="我要跟单"/>';
        infoStr += '<input name="btn_HouseCensor" type="button" class="btn_pon" value="房源变更"/>';
        infoStr += '<input name="btn_delHouse" type="button" class="btn_pon" value="删除房源"/>';
        infoStr += '</li>';
        infoStr += '</ul>';
        infoStr += '</div>';
        infoStr += '<div class="gdtab"><div class="outborder">';
        infoStr += '<div class="infos intitss"><span class="rb"><b>跟单内容</b></span><span class="rbpsn"><b>跟单人</b></span><span class="rbtim"><b>跟单时间</b></span></div>';
        infoStr += '<div id="housetrace_table"></div>';
        infoStr += '</div></div></td</tr>'; //<div id="housetrace_page"></div>
        $('#' + this.currentId).after(infoStr);
    },
    createTraceList: function(count) {
        if (!this.tracePage) {
            this.tracePage = new Chenxin.UI.Pagination({
                id: 'housetrace',
                renderTo: 'housetrace_table',
                actionUrl: this.actionUrl + '?op=gethousetracelist',
                data: { houseid: this.currentId }
            });
        } else {
            this.tracePage.recordCount = 0;
            this.tracePage.hasCount = false;
            this.tracePage.reload({ houseid: this.currentId });
        }
    },
    initEvent: function() {
        var _this = this;
        $('.export').live('click', function() {
            if ($("#isExport").val() == 'can') {
                Chenxin.ajax({
                    type: "post",
                    dataType: "text",
                    paramType: 'json',
                    url: 'service/houseCensor.ashx',
                    data: { op: 'export' },
                    onSuccess: function(data) {
                        _this.exportData();
                    }
                });
            } else {
                alert('请勾选可导出选项，查询后再执行本操作！');
            }
        });

        //查询菜单点击展开事件
        $(".arrow").click(function() {
            $("#panel").slideToggle("slow");
            $(this).toggleClass("up"); return false;
        });

        $('.tr_houselist').live('click', function() {
            $('.tr_houselist,.selected_orgbgcolor').each(function() {
                $(this).removeClass('selected_orgbgcolor');
            });

            $(this).toggleClass('selected_orgbgcolor');
            if (!$('#tr-house-info').length) {
                _this.currentId = this.id;
                _this.loadHouseInfo();

            } else {
                $('#tr-house-info').remove();
                if (_this.currentId != this.id) {
                    _this.currentId = this.id;
                    _this.loadHouseInfo();
                }
            }
        });
        $('.tr_houselist').live('mouseover', function() {
            $(this).toggleClass('orgbgcolor');
        });
        $('.tr_houselist').live('mouseout', function() {
            $(this).toggleClass('orgbgcolor');
        });

        $("input[name='btn_viewownerinfo']").live('click', function() {
            _this.showCustomerInfo();
        });
        $("input[name='btn_trace']").live('click', function() {

            _this.showTraceWin();

        });
        $("input[name='btn_HouseCensor']").live('click', function() {

            _this.showChangeConfirm();

        });
        $("input[name='btn_delHouse']").live('click', function() {

            _this.del();

        });
    },
    showCustomerInfo: function() {
        var _this = this;
        if (!this.HouseCustomerForm) {
            this.HouseCustomerForm = new Chenxin.UI.HouseCustomerForm({
                alwaysCheckPermit: true,
                op: 'view',
                scope: this,
                beforeFitData: function(model) {
                    _this.HouseCustomerForm.currentHouseInfo = _this.currentHouseInfo;
                }
            });
        }
        if (this.HouseCustomerForm.show('view', { currentId: this.currentHouseInfo.CustomerId })) {
            this.getModel(this.currentHouseInfo.HouseId, this.HouseCustomerForm.fitData, this.HouseCustomerForm, 'service/CustomerMgr.ashx', 'getmodel', { flg: 'house' });
            this.HouseCustomerForm.setTitle('查看业主信息');
        }
    },
    /**
    *创建表单
    */
    //	createForm:function(){
    //		return new Chenxin.UI.HouseBaseInfoForm({
    //			scope:this,
    //		});
    //	},
    /**
    *@description 请求变更/变更确认
    */
    showChangeConfirm: function() {
        var _this = this;
        if (!this.currentId) {
            alert('请先选择一条房源记录！');
            return;
        }
        if (!this.changeConfirmWin) {
            this.changeConfirmWin = new Chenxin.UI.ChangeForm({
                currentId: this.currentId,
                scope: this,
                afterGetFormJson: function(json) {
                    this.changeConfirmWin.op = "Edit";
                    delete json.AddPerson
                    delete json.AddDepart
                    delete json.Address;
                    delete json.nowStatus;
                    if (this.changeConfirmWin.requestUser) {
                        json.RequestUserId = this.changeConfirmWin.requestUser.UserId;
                        json.RequestDepartId = this.changeConfirmWin.requestUser.DepartId;
                        json.RequestUserCode = this.changeConfirmWin.requestUser.UserCode;
                    }
                    if (json.ChangeType == '10') {//状态变更，无需传递此参数
                        if (json.RequestDepartId) {
                            delete json.RequestDepartId;
                        }
                        if (json.RequestUserId) {
                            delete json.RequestUserId;
                        }
                        if (json.RequestDepart) {
                            delete json.RequestDepart;
                        }
                        if (json.RequestUser) {
                            delete json.RequestUser;
                        }
                        if (json.RequestUserCode) {
                            delete json.RequestUserCode;
                        }
                    } else if (json.ChangeType == '20') {//人员变更，无需传递此参数	
                        if (json.HouseStatus) {
                            delete json.HouseStatus;
                        }
                    }
                    json.Mobile = json.request_customerMoblie;
                    json.Phone = json.request_customerPhone;
                    json.CustomerName = json.request_customerName;
                    delete json.request_customerName
                    delete json.request_customerPhone
                    delete json.request_customerMoblie

                    json.HouseId = _this.currentId;
                }
            });
        }

        this.changeConfirmWin.setTitle('房源变更');
        var _data = {};
        Chenxin.apply(_data, this.currentHouseInfo);
        _data.AddPerson = this.currentHouseInfo.OprPerson;
        _data.AddDepart = this.currentHouseInfo.OprDepart;
        _data.HouseStatus = _data.Status;
        _data.Remarks = '';
        var temp = this.currentHouseInfo.Status;
        if (temp == 10)
            temp = '有效';
        else if (temp == 20)
            temp = '暂缓';
        else if (temp == -1)
            temp = '无效';
        else if (temp == 30)
            temp = '封盘';
        else
            temp = '';
        _data.nowStatus = temp;
        this.changeConfirmWin.fitData(_data);
        $("#request_tr1").show();
        $("#request_tr2").hide();
        $("#request_tr3").hide();
        $("#request_tr4").hide();
        $("#request_tr5").hide();
        $("#request_tr6").hide();
        $("#request_tr7").hide();
        this.changeConfirmWin.show();
        this.changeConfirmWin.currentId = this.currentId;
    },

    /**
    *@description 跟单
    */
    showTraceWin: function() {
        var _this = this;
        if (!this.traceWin) {
            this.traceWin = new Chenxin.UI.TraceForm({
                afterGetFormJson: function(json) {
                    delete json.AddPerson;
                    delete json.AddDepart;
                    json.HouseAddress = json.Address;
                    json.HouseId = _this.currentId;
                    delete json.Address;

                    if (this.isNotice) {
                        this.additionalParam.noticemodel = {
                            body: json.body,
                            NoticeTime: json.SendDate,
                            HouseId: _this.currentId,
                            HouseAddress: _this.currentHouseInfo.Community + _this.currentHouseInfo.BuildingName + _this.currentHouseInfo.Room
                        };
                        delete json.body;
                        delete json.SendDate;
                    } else {
                        this.additionalParam = {};
                    }
                    this.additionalParam.isnotice = json.isnotice || '0';
                    delete json.isnotice;
                }
            });
        }
        this.traceWin.show();
        $('#tr_noticetime').hide();
        $('#tr_noticecontent').hide();
        var _data = {};
        Chenxin.apply(_data, this.currentHouseInfo);
        _data.AddPerson = this.currentHouseInfo.OprPerson;
        _data.AddDepart = this.currentHouseInfo.OprDepart;
        this.traceWin.fitData(_data);
        this.traceWin.setTitle('房源跟单');
    },

    createSearch: function() {
        return new Chenxin.UI.HouseManageSearch({
            scope: this
        });
    },
    initComponent: function() {
        Chenxin.UI.HouseCensor.superclass.initComponent.call(this);
    }
});

/**
*查询面板
*/
Chenxin.UI.HouseManageSearch=Chenxin.extend(Chenxin.UI.SearchCommon,{
		id:'house_search',
	initEvent:function(){
		var _this=this;
		$('#btn_search').click(function(){
			_this.scope.reloadGrid();
		});
		$('.a-qucik-search').click(function(){
			_this.scope.reloadGrid({quicktype:this.id});
			$(".menu_list").hide(1,function(){$(this).parent(".tlBtn").removeClass("click")});
		});
      //小区自动填充
        $('#txtHousing').autocomplete({
            deferRequestBy: 5,
            width: 200,
            serviceUrl: this.scope.actionUrl + '?op=loadcommunity'
        });
        $('#txtHousing').defText({
            deftxt: '输入楼盘名称'
        });
        $('#daybegin').defText({
            deftxt: '输入开始时间'
        })
        $('#dayend').defText({
            deftxt: '输入结束时间'
        })
        $('#keyword').defText({
            deftxt: '输入地址/编号/楼盘'
        })
        $('#txtPerson').defText({
            deftxt: '输入工号'
        })
        $('#lowprice').defText({
            deftxt: '单位:万'
        });
        $('#heightprice').defText({
            deftxt: '单位:万'
        });
	},
	createComboList: function() {

	    var region = new Chenxin.UI.ParameterCombo({
	        name: 'region',
	        containerId: '#region',
	        key: 'System.Region',
	        tbxWidth: 45,
	        inModelType: 'txt',
	        param: { filter: 'bycity' },
	        onSelected: function() {
	            if (this.getValue() == this.defVal) {
	                block.clearItems();
	            } else {
	                block.param.subkey = this.getValue();
	                block.param.recordid = this.getValueRecord()['id'];
	                block.reload();
	            }
	        }
	    });
	    var block = new Chenxin.UI.ParameterCombo({
	        containerId: '#block',
	        key: 'System.Block',
	        tbxWidth: 45,
	        colWidth: 90,
	        inModelType: 'txt',
	        autoLoad: false
	    });
	
		var status=new Chenxin.UI.Combo({
				containerId:'#status',
				tbxWidth:45,
				items: comboCommon.houseStatus1
			});
		var business=new Chenxin.UI.Combo({
			containerId:'#business',
				tbxWidth:45,
			items:comboCommon.houseBusiness
		});
		var houseuse=new Chenxin.UI.ParameterCombo({
				containerId:'#houseuse',
				key:'House.Use',
				autoLoad:true,
				tbxWidth: 45,
				inModelType: 'txt',
				paramType:'text'		
			});
		var buildingtype=new Chenxin.UI.ParameterCombo({
				containerId:'#buildingtype',
				key:'Building.Type',
				autoLoad:true,
				tbxWidth: 45,
				inModelType: 'txt',
				paramType:'text'			
			});
		var daytype=new Chenxin.UI.Combo({
			containerId:'#daytype',
			tbxWidth: 45,
			initValue: '20',
			items:comboCommon.dateType
		}); 
		
		var markStatus=new Chenxin.UI.Combo({
			containerId:'#marked',
			tbxWidth:45,
			items:comboCommon.MarkStatus
		}); 
		
		var protectedStatus=new Chenxin.UI.Combo({
			containerId:'#protected',
				tbxWidth:45,
			items:comboCommon.ProtectedStatus
		});

		var timetrace = new Chenxin.UI.Combo({
		    containerId: '#timetrace',
		    tbxWidth: 45,
		    items: comboCommon.TimeTrace
		}); 
		
		var combl=new Chenxin.UI.StructComboList({
			cityAfterSelected:function(val){
				if(val==combl.city.defVal){
					region.clearItems();
				}else{
					region.param.cityid=val;
					region.reload();
				}
				block.clearItems();
			}
		});
		var structs = combl.comboList;
		structs.push(timetrace);
		structs.push(region);
		structs.push(block);
		structs.push(status);
		structs.push(business);
		structs.push(houseuse);
		structs.push(buildingtype);
		structs.push(daytype);
		structs.push(markStatus);
		structs.push(protectedStatus);
		
		return structs;
	},
	initComponent:function(){
		Chenxin.UI.HouseManageSearch.superclass.initComponent.call(this);
	}
});


/**
*电话保护
*/
Chenxin.UI.ProtectionForm=Chenxin.extend(Chenxin.UI.FormCommon,{	
	actionUrl:'service/protectPhoneHandler.ashx',
	id:'protectionWin',
	renderTo:'jqm_protection',
	winCls:'',
	initFormValidator:function(){
		$("#protectPhone").formValidator({ validatorgroup: this.id,
	        onshow: "请输入电话！", onfocus: "请输入电话", oncorrect: " "
	    }).inputValidator({
	        min: 1, empty: { leftempty: false, rightempty: false, emptyerror: "电话不能为空!" }, onerror: "电话不能为空!"
	    }).functionValidator({ //电话验证
	        fun: function(val, elem) { return (val.isTel() || val.isMobile()); }, onerror: "请输入正确的电话"
	    });
	},
	createForm:function(){
		return formCommon.houseProtectionForm('protection');
	},
	initEvent:function(){
		var _this=this;
		$('#protection_Save').live('click',function(){
			_this.save();
		});
		$('#protection_Cancle').live('click',function(){
			_this.hide();
		});	
	},
	initComponent:function(){
		Chenxin.UI.ProtectionForm.superclass.initComponent.call(this);
	}
});


$(document).ready(function(){
	var _this=this;
	this.houseCensor=new Chenxin.UI.HouseCensor();	
});

function setunitprice(){
	if($('#wtjzmj').val()&&$('#wtfwzj').val())
	{
		$('#wtfwdj').val(parseFloat($('#wtfwzj').val())/parseFloat($('#wtjzmj').val()));
	}
}



/**
*请求变更/变更确认
*/
Chenxin.UI.HouseCensorForm=Chenxin.extend(Chenxin.UI.FormCommon,{
	actionUrl:'service/ConfirmRequestChange.ashx',
	id:'changeConfirmationWin',
	renderTo:'jqm_changeconfirmation',
	saveSucc:function(){
		this.reloadGrid();
	},
	initFormValidator:function(){
		_this=this;
		$("#requestChangePersonId").formValidator({ validatorgroup:this.id, onshow: "请输入工号", onfocus: "输入工号", oncorrect: "输入正确" }).ajaxValidator({
	        url: "service/ConfirmRequestChange.ashx",
	        datatype: "json",
	        data: "houseid=" + _this.currentId + "&op=validate",
	        success: function(data) {
				if(data.success==0){
	                return false;
				}else{	
					_this.requestUser=data.data.model;	
					
					if(data.success==0){
						$('#request_name').val('');
						$('#request_department').val('');
						return false;
					}else{		
						$('#request_name').val(data.data.model.UserName);
						$('#request_department').val(data.data.model.BelongDepart);
						return true;
					}
				}
	        },
	        buttons: $("#requestChangeSubmitButton"),
	        error: function() { alert("服务器没有返回数据，可能服务器忙，请重试"); },
	        onerror: "员工号不存在",
	        onwait: "正在校验，请稍候..."
	    });
	},
	createForm:function(){
		return formCommon.houseCensorForm("changeconfirmation");		
	},
	createComboList:function(){
		var contactType=new Chenxin.UI.Combo({
			name:'HouseStatus',
			containerId:'#changeconfirmation_requestChangeStyle',
			items:[{txt:'请选择',val:'0'},{txt:'有效',val:'10'},{txt:'无效',val:'-1'},{txt:'暂缓',val:'20'},{txt:'封盘',val:'30'}]
		});
		
		return [contactType];
	},
	initEvent:function(){
		var _this=this;
		$('#changeconfirmation_Save').live('click',function(){
			_this.save();
		});
		$('#changeconfirmation_Cancle').live('click',function(){
			_this.hide();
		});
		$('#changeConfirmationWin input:radio[name="ChangeType"]').live('click',function(){	
			if (this.value == '10') {//状态变更
	            $("#changeconfirmation_requestChangeStyle").removeAttr("disabled");//.attr("disabled", false);
	            $("#requestChangePersonId").attr("disabled", "disabled");
	            $("#requestChangePersonId").val("");
	        } else if (this.value == '20') {//所属人变更
	            $("#changeconfirmation_requestChangeStyle").attr("disabled", "disabled");
	            $("#requestChangePersonId").removeAttr("disabled");
	        } else if (this.value == '30') {
	            $("#changeconfirmation_requestChangeStyle").removeAttr("disabled");
	            $("#requestChangePersonId").removeAttr("disabled");
	        }
		});	
	},
	formValidator:function(model){
		var status=true;
		switch(model.ChangeType){
			case '10':
				status=this.checkCombo();
				break;
			case '20':	
				status=jQuery.formValidator.pageIsValid(this.id);
				break;
			case '30':
				status=jQuery.formValidator.pageIsValid(this.id)&&this.checkCombo()
				break;
			default:			
				status=false;
				break;
		}
		if(!status){			
			alert('请填写完必填(选)项再进行表单提交！');			
		}
		return status;
	},
	initComponent:function(){
		Chenxin.UI.HouseCensorForm.superclass.initComponent.call(this);		
	}
});
